class Example:
    def __init__(self):
        self.value = 42


example = Example()

# 检查属性是否存在
print(hasattr(example, "value"))  # True

# 获取属性值
print(getattr(example, "value"))  # 42

# 设置属性值
setattr(example, "new_attr", "hello")
print(example.new_attr)  # hello

# 删除属性
delattr(example, "new_attr")
print("new_attr" in dir(example))  # False


class ExampleObj:
    def calculate(self, x, y):
        return x + y


obj = ExampleObj()
method_name = "calculate"
args = (3, 5)

if hasattr(obj, method_name):
    method = getattr(obj, method_name)
    if callable(method):
        try:
            result = method(*args)
            print("Result:", result)  # Result: 8
        except Exception as e:
            print(f"调用方法 {method_name} 时发生异常:", e)
    else:
        print(f"{method_name} 不是可调用对象")
else:
    print(f"对象没有方法 {method_name}")


class APIHandler:
    def get_user(self, user_id, include_profile=False):
        user = {"id": user_id, "name": f"User{user_id}"}
        if include_profile:
            user["profile"] = {"email": f"user{user_id}@example.com"}
        return user

    def create_post(self, title, content, tags=None, published=True):
        post = {
            "title": title,
            "content": content,
            "tags": tags or [],
            "published": published,
        }
        return post


def call_method_dynamically(obj, method_name, **kwargs):
    if not hasattr(obj, method_name):
        raise AttributeError(f"对象没有方法 '{method_name}'")

    method = getattr(obj, method_name)
    if not callable(method):
        raise TypeError(f"'{method_name}' 不是可调用方法")

    return method(**kwargs)


# 使用示例
handler = APIHandler()

user_data = call_method_dynamically(
    handler, "get_user", user_id=123, include_profile=True
)
print("用户数据:", user_data)

post_data = call_method_dynamically(
    handler,
    "create_post",
    title="Python反射",
    content="反射机制详解",
    tags=["python", "reflection"],
    published=False,
)
print("文章数据:", post_data)

filename = "non_existent_file.txt"
print(filename[:-4])  # 输出: non_existent_file
import os
import importlib


class PluginManager:
    def __init__(self):
        self.plugins = {}
        self.load_plugins()

    def load_plugins(self):
        """动态加载所有插件"""
        plugins_dir = "plugins"

        for filename in os.listdir(plugins_dir):
            if filename.endswith(".py") and not filename.startswith("_"):
                module_name = filename[:-3]  # 移除 .py
                try:
                    # 动态导入模块
                    module = importlib.import_module(f"{plugins_dir}.{module_name}")

                    # 查找插件类（约定：以Plugin结尾的类）
                    for attr_name in dir(module):
                        attr = getattr(module, attr_name)
                        if (
                            isinstance(attr, type)
                            and attr_name.endswith("Plugin")
                            and attr_name != "PluginManager"
                        ):

                            plugin_instance = attr()
                            self.plugins[plugin_instance.name] = plugin_instance
                            print(
                                f"加载插件: {plugin_instance.name} v{plugin_instance.version}"
                            )

                except Exception as e:
                    print(f"加载插件 {module_name} 失败: {e}")

    def execute_plugin(self, plugin_name, *args, **kwargs):
        """执行插件"""
        if plugin_name in self.plugins:
            plugin = self.plugins[plugin_name]
            return plugin.execute(*args, **kwargs)
        else:
            return f"Error: Plugin '{plugin_name}' not found"

    def list_plugins(self):
        """列出所有可用插件"""
        return list(self.plugins.keys())


# 使用插件系统
manager = PluginManager()
print("可用插件:", manager.list_plugins())

# 动态调用插件
result1 = manager.execute_plugin("Basic Calculator", "add", 10, 5)
result2 = manager.execute_plugin("Greeter", "Alice", formal=True)

print("计算结果:", result1)  # 15
print("问候结果:", result2)  # Good day, Alice. How may I assist you?


class DynamicAttributes:
    """动态属性管理类"""

    def __init__(self):
        self._data = {}
        self._access_count = {}

    def __getattr__(self, name):
        """访问不存在的属性时调用"""
        if name in self._data:
            self._access_count[name] = self._access_count.get(name, 0) + 1
            return self._data[name]
        else:
            raise AttributeError(
                f"'{type(self).__name__}' object has no attribute '{name}'"
            )

    def __setattr__(self, name, value):
        """赋值属性时调用"""
        if name.startswith("_"):
            super().__setattr__(name, value)
        else:
            if "_data" in self.__dict__:
                self._data[name] = value
                self._access_count[name] = 0
            else:
                super().__setattr__(name, value)

    def __delattr__(self, name):
        """删除属性时调用"""
        if name in self._data:
            del self._data[name]
            del self._access_count[name]
        else:
            super().__delattr__(name)

    def __dir__(self):
        """自定义 dir() 输出"""
        base_attrs = super().__dir__()
        dynamic_attrs = list(self._data.keys())
        return base_attrs + dynamic_attrs

    def get_access_stats(self):
        """返回访问次数统计表"""
        return self._access_count.copy()


# 使用示例
obj = DynamicAttributes()
obj.foo = 123
obj.bar = "hello"

print(obj.foo)  # 123
print(obj.bar)  # hello
print(obj.foo)  # 再次访问

del obj.bar

print("所有属性：", dir(obj))
print("访问次数统计：", obj.get_access_stats())


class ConfigManager:
    def __init__(self, config_dict=None):
        self._config = config_dict or {}
        self._defaults = {
            "debug": False,
            "host": "localhost",
            "port": 8080,
            "timeout": 30,
        }

    def __getattr__(self, name):
        """通过属性访问配置"""
        if name in self._config:
            return self._config[name]
        elif name in self._defaults:
            return self._defaults[name]
        else:
            raise AttributeError(f"配置项 '{name}' 不存在")

    def set_config(self, **kwargs):
        """批量设置配置"""
        for key, value in kwargs.items():
            self._config[key] = value

    def show_config(self):
        """显示所有配置"""
        all_config = {**self._defaults, **self._config}
        for key, value in sorted(all_config.items()):
            source = "自定义" if key in self._config else "默认"
            print(f"{key:20} = {value:15} [{source}]")


# 使用示例
config = ConfigManager()

print(f"Host: {config.host}")  # localhost
print(f"Port: {config.port}")  # 8080
print(f"Debug: {config.debug}")  # False

config.set_config(host="127.0.0.1", port=9000, new_setting="custom")

print("\n所有配置:")
config.show_config()


class APIController:
    def get_users(self, user_id=None):
        if user_id:
            return f"获取用户 {user_id}"
        return "获取所有用户"

    def create_user(self, name, email):
        return f"创建用户: {name} ({email})"

    def update_user(self, user_id, **data):
        return f"更新用户 {user_id}: {data}"

    def delete_user(self, user_id):
        return f"删除用户 {user_id}"


class Router:
    def __init__(self):
        self.controller = APIController()

    def route(self, method, path, **params):
        """路由请求到对应方法"""
        # strip("/")指定去除左右两边的/
        path_parts = path.strip("/").split("/")
        print(f"{path} ->  {path_parts}")

        if path_parts[0] == "users":
            if method.upper() == "GET":
                action = "get_users"
                if len(path_parts) > 1:
                    params["user_id"] = path_parts[1]
            elif method.upper() == "POST":
                action = "create_user"
            elif method.upper() == "PUT" and len(path_parts) > 1:
                action = "update_user"
                params["user_id"] = path_parts[1]
            elif method.upper() == "DELETE" and len(path_parts) > 1:
                action = "delete_user"
                params["user_id"] = path_parts[1]
            else:
                return "Error: 无效的请求"

            if hasattr(self.controller, action):
                method_to_call = getattr(self.controller, action)
                return method_to_call(**params)
            else:
                return f"Error: 操作 {action} 不存在"

        return "Error: 路径不存在"


# 使用示例
router = Router()

requests = [
    ("GET", "/users"),
    ("GET", "/users/123"),
    ("POST", "/users", {"name": "Alice", "email": "alice@example.com"}),
    ("PUT", "/users/123", {"name": "Alice Smith"}),
    ("DELETE", "/users/123"),
]

for method, path, *args in requests:
    params = args[0] if args else {}
    result = router.route(method, path, **params)
    print(f"{method} {path} -> {result}")


def safe_getattr(obj, attr_name, default=None):
    """安全地获取对象属性"""
    try:
        return getattr(obj, attr_name)
    except AttributeError:
        return default


# 使用示例
class Example:
    name = "test"
    value = 42


example = Example()
print(safe_getattr(example, "name", "default"))  # test
print(safe_getattr(example, "nonexistent", "default"))  # default


def apply_to_all_attributes(obj, func):
    """将指定函数应用到对象的所有属性上"""
    results = {}
    for attr_name in dir(obj):
        if not attr_name.startswith("__"):
            try:
                attr_value = getattr(obj, attr_name)
                results[attr_name] = func(attr_value)
            except Exception as e:
                results[attr_name] = f"Error: {e}"
    return results


# 使用示例
class Example:
    name = "test"
    value = 42

    # def method(self):
    #     return "result"


def describe_value(value):
    print("Applying to value:", value)
    return f"{type(value).__name__}: {value}"


example = Example()
print(apply_to_all_attributes(example, describe_value))
import time


class Performance:
    def method(self):
        return "result"


obj = Performance()

# 测试直接方法调用与反射方法调用的性能差异
start = time.time()
for _ in range(100000):
    obj.method()
direct_time = time.time() - start

start = time.time()
for _ in range(100000):
    getattr(obj, "method")()
reflect_time = time.time() - start

print(f"直接调用: {direct_time:.6f}s")
print(f"反射调用: {reflect_time:.6f}s")
print(f"性能差异: {reflect_time/direct_time:.2f}x")
